package com.sport.utils;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;

public class PasswordUtils {
    //加密，由盐和明文密码组成
    public static String encrypt(String slat, String password) {
        //生成密钥
        byte[] encoded = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue(), slat.getBytes()).getEncoded();
        //返回加密后的密文
        AES aes = SecureUtil.aes(encoded);
        return aes.encryptBase64(password);
    }

    //解密，由盐和明文密码组成
    public static String decrypt(String slat, String password) {
        //生成密钥
        byte[] encoded = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue(), slat.getBytes()).getEncoded();
        //返回解密后的密文
        AES aes = SecureUtil.aes(encoded);
        return aes.decryptStr(password);
    }


    public static void main(String[] args){
        String slat = RandomUtil.randomString(16);
        String a = encrypt(slat, "123456");
        String b = decrypt(slat, a);
        System.out.println(slat);
        System.out.println(a);
        System.out.println(b);
    }
}
